package com.sz.biz.common.contract.service.impl;

import com.google.common.collect.Lists;
import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.common.contract.entity.ContField;
import com.sz.biz.common.contract.service.ContTypeEnum;
import com.sz.biz.common.contract.service.ContFieldService;
import com.sz.biz.common.contract.dto.ContFieldDto;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.EntityUtils;
import com.sz.biz.logistics.constants.ErrorCodes;
import com.sz.common.core.service.PrincipalLogUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Package: com.sz.biz.common.contract.service.impl<br>
 * Function: 合同field service 实现
 * Author: yanhong.xiao.<br>
 * Date: 2017-07-19 16:28:00.
 */
@Service
public class ContFieldServiceImpl extends AbstractService implements ContFieldService {
    @Override
    protected String getMapperNamespace() {
        return "com.sz.biz.common.contract.entity.ContFieldMapper";
    }

    /**
     * 批量保存合同field
     *
     * @param fieldDtoList
     */
    @Override
    public void batchSave(List<ContFieldDto> fieldDtoList) {
        List<ContField> fieldList = Lists.newArrayList();
        //组装field
        if (CollectionUtils.isNotEmpty(fieldDtoList)) {
            if (checkContFieldDto(fieldDtoList)) {
                for (ContFieldDto dto :
                        fieldDtoList) {
                    ContField field = new ContField();
                    EntityUtils.copyPropertiesIgnoreNull(dto, field);
                    fieldList.add(field);
                }
            }
            //批量保存
            dao.batchInsert(getSqlName("insertSelective"), fieldList);
            //保存操作日志
            Map<String, Object> dataBackup = new HashMap<>();
            dataBackup.put("ADD_CONT_FIELD", fieldList);
            PrincipalLogUtils.addOperationLog(ModuleNames.CONT, ModuleNames.CONT_FIELD, UserActions.ADD, "添加合同中的field", dataBackup);
        }

    }

    /**
     * 根据类型删除对应relIds的field
     *
     * @param contTypeEnum
     * @param relIds
     */
    @Override
    public void batchDeleteByRelIds(ContTypeEnum contTypeEnum, List<Integer> relIds) {
        Map<String, Object> map = new HashedMap();
        map.put("type", Integer.valueOf(contTypeEnum.getCode()));
        if (CollectionUtils.isNotEmpty(relIds)) {
            map.put("relIds", relIds);
        }
        dao.delete(getSqlName("batchDeleteByRelIds"), map);
    }

    /**
     * 根据relId获取field
     *
     * @param contTypeEnum
     * @param relId
     * @return
     */
    @Override
    public List<ContField> getFieldsByRelId(ContTypeEnum contTypeEnum, Integer relId) {
        Map<String, Object> map = new HashedMap();
        map.put("type", ContTypeEnum.getIntegerCode(contTypeEnum));
        map.put("relId", relId);
        List<ContField> fieldList = dao.findForList(getSqlName("getFieldsByRelId"), map, ContField.class);
        return fieldList;
    }

    @Override
    public Map<String, String> getFields(ContTypeEnum contTypeEnum, Integer relId) {
        Map<String, String> map = new HashedMap();
        List<ContField> fieldList = getFieldsByRelId(contTypeEnum, relId);
        if (CollectionUtils.isNotEmpty(fieldList)) {
          fieldList.forEach(contField -> {
              map.put(contField.getFieldKey(),contField.getFieldValue());
          });
        }
        return map;
    }

    @Override
    public List<ContField> getFieldsByRelIds(ContTypeEnum contTypeEnum, List<Integer> relIds) {
        Map<String, Object> map = new HashedMap();
        map.put("type", ContTypeEnum.getIntegerCode(contTypeEnum));
        map.put("relIds", relIds);
        List<ContField> fieldList = dao.findForList(getSqlName("getFieldsByRelIds"), map, ContField.class);
        return fieldList;
    }

    @Override
    public Map<Object, Map<String, String>> getFieldrDtos(List<ContField> fieldList) {
        Map<Object, Map<String, String>> result=new HashedMap();
        if (CollectionUtils.isNotEmpty(fieldList)){
            fieldList.forEach(contField -> {
                Map<String, String> map = new HashedMap();
                map.put(contField.getFieldKey(),contField.getFieldValue());
                result.put(contField.getRelId(),map);
            });
        }
        return result;
    }


    private boolean checkContFieldDto(List<ContFieldDto> fieldDtoList) {
        if (CollectionUtils.isNotEmpty(fieldDtoList)) {
            for (ContFieldDto dto :
                    fieldDtoList) {
                boolean typeFlag = true;
                ContTypeEnum typeEnum = ContTypeEnum.CONT_TEMPLATE;
                if (null == dto.getType()) {
                    typeFlag = false;
                } else {
                    typeEnum = ContTypeEnum.valueOfEnum(String.valueOf(dto.getType()));
                    if (null == typeEnum) {
                        typeFlag = false;
                    }
                }
                if (!typeFlag) {
                    throw Exceptions.bizException(ErrorCodes.ERROR_COMMON, "合同字段的类型不正确");
                }

                if (null == dto.getRelId() || dto.getRelId() == 0) {
                    if (typeEnum == ContTypeEnum.CONT_TEMPLATE) {
                        throw Exceptions.bizException(ErrorCodes.ERROR_COMMON, "合同字段对应的合同模板无效");
                    }
                    if (typeEnum == ContTypeEnum.CONT_CONTRACTOR) {
                        throw Exceptions.bizException(ErrorCodes.ERROR_COMMON, "合同字段对应的合同无效");
                    }
                }
            }

        }

        return true;
    }
}
